﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Management
{
    public class SearchManagementModel
    {
        [DisplayName("Buscar por Nombre")]
        [StringLength(150, ErrorMessage = "El nombre debe ser máximo de 150 caracteres")]
        [RegularExpression(@"^[a-zA-Z0-9 \(\)ñÑ\-.,áéíóúÁÉÍÓÚ]+$", ErrorMessage = "El nombre debe ser solo letras mayusculas o minusculas o numeros")]
        public string Pattern { get; set; }

        public List<SearchManagementInfo> ManagementInfo
        {
            get
            {
                return managementInfo;
            }
        }

        private List<SearchManagementInfo> managementInfo;

        public SearchManagementModel()
        {
            SearchManagement();
        }

        public void SearchManagement(String pattern)
        {
            riskanalysisEntities entities = new riskanalysisEntities();
            var managements = from m in entities.managements
                             where m.name.Contains(pattern) && m.enabled
                             select new {
                                 ManagementId = m.managementId,
                                 Name = m.name, 
                                 Responsible = (m.user.firstName + " " +m.user.lastName +  " "  + m.user.secondLastName),
                                 Parent = m.management2.name};
            managementInfo = new List<SearchManagementInfo>();

            foreach (var man in managements)
            {
                managementInfo.Add(
                    new SearchManagementInfo
                    {
                        Name = man.Name,
                        Responsible = man.Responsible,
                        Parent = man.Parent,
                        ManagementId = man.ManagementId
                    });
            }  
            
        }

        public void SearchManagement()
        {
            SearchManagement("");
        }
    }
}